package com.shaozi.nc.manage.observer.base;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;

import com.shaozi.data.mq.business.base.SourceTypeConsts.OA.Attendance;
import com.shaozi.data.mq.business.observer.MqTargetObserverBean;
import com.shaozi.data.mq.business.utils.MqUtil;
import com.shaozi.lib.throwable.SzException;
import com.shaozi.nc.bean.resp.ReqMessageSetReadForTypeBean;
import com.shaozi.nc.bean.resp.RespMessageSetReadForTypeBean;
import com.shaozi.nc.manage.secretary.SecretaryManage;
import com.shaozi.nc.manage.secretary.bean.UpdateUserReadBean;
import com.shaozi.nc.mq.bean.attendance.AttendanceNoticesBean;
import com.shaozi.nc.mq.bean.base.BizBaseBean;
import com.shaozi.utils.common.BeanUtils;

/**
 * 通用小秘书观察者 设置已读 基础类
 * 
 * @author lyx
 *
 */
public class BaseSecretaryObserver {
	private Logger logger = Logger.getLogger(BaseSecretaryObserver.class);

	protected void updateUserRead(Integer fromSourceType, MqTargetObserverBean mqBean, Integer[] sourceType) {
		if (mqBean.getBiz() instanceof BizBaseBean) {
			Integer[] extraIds = null;
			if (Attendance.Notices == mqBean.getSourceType()) {
				AttendanceNoticesBean obj = (AttendanceNoticesBean) mqBean.getBiz();
				if (obj == null) {
					return;
				}
				List<Integer> ids = new ArrayList<Integer>();
				if (obj.getInIds() != null) {
					for (Integer id : obj.getInIds()) {
						ids.add(id);
					}
				}
				if (obj.getOutIds() != null) {
					for (Integer id : obj.getOutIds()) {
						ids.add(id);
					}
				}
				extraIds = ids.toArray(new Integer[ids.size()]);
			} else {
				BizBaseBean obj = (BizBaseBean) mqBean.getBiz();
				extraIds = new Integer[] { obj.getId() };
			}
			String userId = mqBean.getUserId();
			if (extraIds != null && extraIds.length != 0 && !StringUtils.isEmpty(userId)) {
				try {
					logger.info("from srouceType:" + mqBean.getSourceType() + ",reportIds:"
							+ String.valueOf(Arrays.toString(extraIds)) + ",sourceType:" + Arrays.toString(sourceType)
							+ ",userId:" + userId + ",companyId:" + mqBean.getCompanyId());

					int moduleType = MqUtil.getBaseSourceType(fromSourceType);
					String companyId = mqBean.getCompanyId();
					SecretaryManage manange = (SecretaryManage) BeanUtils.getBean("secretaryMgr");

					ReqMessageSetReadForTypeBean reqBean = new ReqMessageSetReadForTypeBean();
					reqBean.setModuleType(moduleType);
					reqBean.setCompanyId(companyId);
					reqBean.setUserId(userId);

					RespMessageSetReadForTypeBean respBean = new RespMessageSetReadForTypeBean();
					respBean.setUpdateTime(System.currentTimeMillis());

					UpdateUserReadBean bean = new UpdateUserReadBean(extraIds, sourceType, userId, companyId);
					manange.makeSecretaryReadByObserver(reqBean, respBean, bean);
				} catch (SzException e) {
					e.printStackTrace();
				}
			}
		}
	}

}